﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;
using EntityModel;

namespace MyFluentNHibernate
{
    public class ProductMap : ClassMap<Product>
    {
        public ProductMap()
        {
            Id(p => p.ProductID);
            HasManyToMany<Order>(p => p.Orders)
                .AsSet()
                .LazyLoad()
                .ParentKeyColumn("ProductID")
                .ChildKeyColumn("OrderID")
                .Table("OrderProduct");

            Map(p => p.CreateTime);
            Map(p => p.Name);
            Map(p => p.Price);
        }
    }

    public class OrderMap : ClassMap<Order>
    {
        public OrderMap()
        {
            Id(o => o.OrderID).GeneratedBy.Identity();
            HasManyToMany<Product>(o => o.Products)
                .AsSet()
                .Not.LazyLoad()
                .Cascade.All()
                .ParentKeyColumn("OrderID")
                .ChildKeyColumn("ProductID")
                .Table("OrderProduct");

            Map(o => o.Price);
            Map(o => o.State).CustomType<OrderState>();
            Map(o => o.Address);
            Map(o => o.Coignee);
            Map(o => o.CreateTime);
            Map(o => o.Zip);
            References<User>(o => o.User).Not.LazyLoad().Column("UserID");
        }
    }

    public class BookMap : SubclassMap<BookProduct>
    {
        public BookMap()
        {
            Table("Book");
            KeyColumn("ProductID");
            Map(p => p.Author);
        }
    }

    public class MobileMap : SubclassMap<MobileProduct>
    {
        public MobileMap()
        {
            Table("Mobile");
            KeyColumn("ProductID");
            Map(p => p.Brand);
            Map(p => p.Number);
        }
    }
}
